import json
import time

from huey.api import Task
from huey.signals import SIGNAL_EXECUTING, SIGNAL_ERROR, SIGNAL_LOCKED, SIGNAL_CANCELED, SIGNAL_REVOKED, \
    SIGNAL_COMPLETE, SIGNAL_INTERRUPTED
from src.huey_init._huey import huey
from src.huey_init.post_status import post_s
from src.huey_init.storages import Storage
from src.huey_init.pgsql import db_manager
from datetime import datetime
from zoneinfo import ZoneInfo


tz = ZoneInfo("Asia/Shanghai")

table_name = "public.task_status"
storage = Storage()
@huey.signal(SIGNAL_EXECUTING)
def task_running_handler(signal, task:Task):
    # print("write")
    # print("huey_status:{}".format(task.id))
    # storage.sets("huey_status:{}".format(task.id),{"status": "running"})
    storage.sets("running_tasks:{}".format(task.id),{"id":task.id},7200)
    # storage.write('started', task.id)
    arg =  task.args
    # kwg = task.kwargs
    # print("arg：{}".format(arg))
    # with open("logs.txt","w",encoding="utf-8") as f:
    #     f.write("arg：\n")
    #     f.write(str(arg))
    #     f.write("\n")
    #     f.write("kwg：\n")
    #     f.write(str(kwg))
    ex_infos = arg[0].exchange_info
    dmsTaskId = ex_infos.get("dmsTaskId")
    status = 2
    functionType = ex_infos.get("functionType")
    time.sleep(2)
    post_s(functionType, status, dmsTaskId)
    db_manager.insert(table_name, ['task_id', 'status_id', 'status', 'start_time'], [task.id, '0', 'running', datetime.now(tz)])

@huey.signal(SIGNAL_ERROR)
def task_not_executed_handler(signal, task, exc=None):
    # print("write")
    # print("huey_status:{}".format(task.id))
    # storage.sets("huey_status:{}".format(task.id),  {"status": "error"})
    storage.sets("revoke_tasks:{}".format(task.id), {"id": task.id}, 43200)
    storage.delete("running_tasks:{}".format(task.id))
    db_manager.update(table_name, {"status_id": "2", "status": "error", "end_time":datetime.now(tz)}, {"task_id": task.id})
    arg = task.args
    ex_infos = arg[0].exchange_info
    dmsTaskId = ex_infos.get("dmsTaskId")
    status = 9
    functionType = ex_infos.get("functionType")
    post_s(functionType, status, dmsTaskId)

@huey.signal(SIGNAL_CANCELED)
def task_cancel_handler(signal, task, exc=None):
    # print("write")
    # print("huey_status:{}".format(task.id))
    storage.delete("running_tasks:{}".format(task.id))
    storage.delete("revoke_tasks:{}".format(task.id))
    arg = task.args
    ex_infos = arg[0].exchange_info
    dmsTaskId = ex_infos.get("dmsTaskId")
    status = 10
    functionType = ex_infos.get("functionType")
    post_s(functionType, status, dmsTaskId)

@huey.signal(SIGNAL_REVOKED)
def task_revoke_handler(signal, task, exc=None):
    storage.delete("running_tasks:{}".format(task.id))
    storage.delete("revoke_tasks:{}".format(task.id))
    arg = task.args
    ex_infos = arg[0].exchange_info
    dmsTaskId = ex_infos.get("dmsTaskId")
    status = 10
    functionType = ex_infos.get("functionType")
    post_s(functionType, status, dmsTaskId)

@huey.signal(SIGNAL_COMPLETE)
def task_success(signal, task, exc=None):
    # print("write")
    # print("huey_status:{}".format(task.id))
    # storage.sets("huey_status:{}".format(task.id), {"status": "success"})
    storage.delete("running_tasks:{}".format(task.id))
    db_manager.update(table_name, {"status_id": "1", "status": "success", "end_time":datetime.now(tz)}, {"task_id": task.id})
    arg = task.args
    ex_infos = arg[0].exchange_info
    dmsTaskId = ex_infos.get("dmsTaskId")
    status = 3
    functionType = ex_infos.get("functionType")
    post_s(functionType, status, dmsTaskId)

@huey.signal(SIGNAL_INTERRUPTED)
def task_interrupted(signal, task, exc=None):
    storage.delete("running_tasks:{}".format(task.id))
    db_manager.update(table_name, {"status_id": "3", "status": "interrupt", "end_time":datetime.now(tz)}, {"task_id": task.id})
    arg = task.args
    ex_infos = arg[0].exchange_info
    dmsTaskId = ex_infos.get("dmsTaskId")
    status = 10
    functionType = ex_infos.get("functionType")
    post_s(functionType, status, dmsTaskId)